// var wx={}
module.exports = {
    $http(params:object, method:any, api_path:string) {
        const header={
            token:""
        }
        if (wx.getStorageSync("token")) {
            header.token = wx.getStorageSync("token")
        }
        return new Promise((resolve, reject) => {
            wx.request({
                url: api_path,
                // data: Object.assign({}, params.data),
                data: {
                    ...params,
                    // ...header
                },
                header,
                method: method,
                success: resolve,
                fail: reject
            })
        })
    },
    $upFile(options:any) {
        const { name="api" , path: filePath} = options
        const promise= new Promise((resolve, reject) => {
            wx.uploadFile({
                url:wx.$file_url,
                filePath,name,
                header: {
                    'content-type': 'multipart/form-data'
                },
                success: function (res) {
                    console.log(res);
                    // console.log(JSON.parse(res.data));
                    
                    let imgArr = '' + JSON.parse(res.data).data.src;
                    console.log(imgArr);
                    resolve(imgArr)
                },
                fail:(res)=>{
                    reject(res)
                }
            });
        })
        
        return promise.then(res=>{
            return res;
        })


    },
    $request(params:any, path:string="",method:string="POST") {
        let url = path.startsWith('http') ? path : wx.$url + path
        return this.$http(
            params,
            method,
            url
        ).then((res: any) =>{
            // console.log(res);
            if (res.statusCode==200) {
                return res.data
            }else{
                return { }
            }
        }, (err: any) => {
            wx.showModal({
                title: '提示',
                content: err.errMsg,
                showCancel: false,
                cancelText: '取消',
                cancelColor: '#000000',
                confirmText: '确定',
                confirmColor: '#3CC51F',
                success: (result) => {
                    if(result.confirm){
                      
                    }
                },
                fail: ()=>{},
                complete: ()=>{}
            });
            return false
        })
    }
    
}